```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Elasticsearch 脚本功能深度解析 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #3a7bd5 0%, #00d2ff 100%);
        }
        .code-block {
            background-color: #f6f8fa;
            border-left: 4px solid #3a7bd5;
        }
        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }
        .mermaid-tooltip {
            position: absolute;
            background: #fff;
            padding: 8px 12px;
            border-radius: 4px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            font-size: 14px;
            z-index: 100;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <header class="hero-gradient text-white py-20 px-4">
        <div class="container mx-auto max-w-6xl">
            <div class="flex flex-col items-center text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6">Elasticsearch 脚本功能全解析</h1>
                <p class="text-xl md:text-2xl max-w-3xl mb-8 opacity-90">探索 Painless 脚本的强大能力，解锁复杂查询与数据处理的新维度</p>
                <div class="flex space-x-4">
                    <a href="#painless-basics" class="px-6 py-3 bg-white text-blue-600 rounded-lg font-medium hover:bg-gray-100 transition duration-300">
                        <i class="fas fa-book mr-2"></i>开始学习
                    </a>
                    <a href="#examples" class="px-6 py-3 bg-transparent border-2 border-white text-white rounded-lg font-medium hover:bg-white hover:text-blue-600 transition duration-300">
                        <i class="fas fa-code mr-2"></i>查看示例
                    </a>
                </div>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <main class="container mx-auto max-w-6xl px-4 py-12">
        <!-- Introduction Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-lg p-8">
                <p class="text-lg text-gray-700 leading-relaxed">
                    Elasticsearch 的脚本功能允许用户在查询和数据操作中自定义逻辑，提供了灵活性以应对复杂的需求。Elasticsearch 提供了多种脚本语言，其中 <span class="font-bold text-blue-600">Painless</span> 是最常用的脚本语言，它专为 Elasticsearch 设计，具有较高的性能和安全性。
                </p>
            </div>
        </section>

        <!-- Painless Basics Section -->
        <section id="painless-basics" class="mb-16">
            <div class="flex items-center mb-8">
                <div class="bg-blue-100 p-3 rounded-full mr-4">
                    <i class="fas fa-code text-blue-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">Painless 脚本基础</h2>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6 mb-8">
                <div class="feature-card bg-white rounded-xl shadow-md p-6 transition duration-300">
                    <div class="text-blue-500 mb-4">
                        <i class="fas fa-bolt text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">高效执行</h3>
                    <p class="text-gray-600">优化了执行性能，适合在 Elasticsearch 中执行大量的脚本操作。</p>
                </div>
                <div class="feature-card bg-white rounded-xl shadow-md p-6 transition duration-300">
                    <div class="text-green-500 mb-4">
                        <i class="fas fa-shield-alt text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">安全性高</h3>
                    <p class="text-gray-600">默认禁用不安全的操作，如访问文件系统和网络，防止潜在的安全问题。</p>
                </div>
                <div class="feature-card bg-white rounded-xl shadow-md p-6 transition duration-300">
                    <div class="text-purple-500 mb-4">
                        <i class="fas fa-pen-fancy text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">语法简洁</h3>
                    <p class="text-gray-600">语法类似于 Java，但去除了部分复杂性，使得编写和阅读脚本更加简洁。</p>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-lg overflow-hidden">
                <div class="p-6 border-b border-gray-200 flex items-center">
                    <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                    <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                    <div class="w-3 h-3 rounded-full bg-green-500"></div>
                </div>
                <div class="p-6">
                    <div class="code-block p-4 rounded-lg mb-4 overflow-x-auto">
                        <pre class="text-sm text-gray-800">
// 简单的Painless脚本示例
def total = doc['price'].value * doc['quantity'].value;
if (total > 1000) {
    return total * 0.9; // 大额订单9折
}
return total;</pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Mermaid Diagram -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8">Painless 脚本应用场景</h2>
            <div class="bg-white rounded-xl shadow-lg p-6">
                <div class="mermaid">
                    graph TD
                        A[Painless脚本] --> B[查询中]
                        A --> C[聚合中]
                        A --> D[更新中]
                        B --> E[自定义评分]
                        B --> F[动态字段]
                        C --> G[加权计算]
                        C --> H[复杂聚合]
                        D --> I[修改文档]
                        D --> J[条件更新]
                </div>
            </div>
        </section>

        <!-- Query Examples Section -->
        <section id="examples" class="mb-16">
            <div class="flex items-center mb-8">
                <div class="bg-purple-100 p-3 rounded-full mr-4">
                    <i class="fas fa-search text-purple-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">在查询中使用 Painless 脚本</h2>
            </div>

            <div class="bg-white rounded-xl shadow-lg overflow-hidden mb-8">
                <div class="p-6 border-b border-gray-200 flex items-center justify-between">
                    <div class="flex items-center">
                        <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500"></div>
                    </div>
                    <span class="text-sm font-medium text-gray-500">查询示例</span>
                </div>
                <div class="p-6">
                    <h3 class="text-xl font-semibold mb-4">自定义评分示例</h3>
                    <div class="code-block p-4 rounded-lg mb-4 overflow-x-auto">
                        <pre class="text-sm text-gray-800">
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "title": "Elasticsearch"
        }
      },
      "script_score": {
        "script": {
          "source": "Math.log(2 + doc['popularity'].value)"
        }
      }
    }
  }
}</pre>
                    </div>
                    <div class="flex items-start mt-4">
                        <div class="bg-blue-100 p-2 rounded-full mr-3">
                            <i class="fas fa-info-circle text-blue-600"></i>
                        </div>
                        <p class="text-gray-600 flex-1">
                            上面的查询中，<code class="bg-gray-100 px-1 rounded">function_score</code> 查询用于根据脚本计算的分数来调整文档的评分。
                            <code class="bg-gray-100 px-1 rounded">script_score</code> 使用 Painless 脚本 <code class="bg-gray-100 px-1 rounded">Math.log(2 + doc['popularity'].value)</code> 来计算评分。
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Aggregation Examples Section -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="bg-green-100 p-3 rounded-full mr-4">
                    <i class="fas fa-chart-bar text-green-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">在聚合中使用 Painless 脚本</h2>
            </div>

            <div class="bg-white rounded-xl shadow-lg overflow-hidden mb-8">
                <div class="p-6 border-b border-gray-200 flex items-center justify-between">
                    <div class="flex items-center">
                        <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500"></div>
                    </div>
                    <span class="text-sm font-medium text-gray-500">聚合示例</span>
                </div>
                <div class="p-6">
                    <h3 class="text-xl font-semibold mb-4">加权总和计算</h3>
                    <div class="code-block p-4 rounded-lg mb-4 overflow-x-auto">
                        <pre class="text-sm text-gray-800">
{
  "aggs": {
    "total_weighted_score": {
      "sum": {
        "script": {
          "source": "doc['score'].value * params.weight",
          "params": {
            "weight": 1.5
          }
        }
      }
    }
  }
}</pre>
                    </div>
                    <div class="flex items-start mt-4">
                        <div class="bg-blue-100 p-2 rounded-full mr-3">
                            <i class="fas fa-info-circle text-blue-600"></i>
                        </div>
                        <p class="text-gray-600 flex-1">
                            这个聚合中，<code class="bg-gray-100 px-1 rounded">sum</code> 聚合使用脚本 <code class="bg-gray-100 px-1 rounded">doc['score'].value * params.weight</code> 来计算字段 <code class="bg-gray-100 px-1 rounded">score</code> 的加权总和。<br>
                            <code class="bg-gray-100 px-1 rounded">params.weight</code> 是传递给脚本的参数，用于动态调整加权值。
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Update Examples Section -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="bg-yellow-100 p-3 rounded-full mr-4">
                    <i class="fas fa-sync-alt text-yellow-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">在更新中使用 Painless 脚本</h2>
            </div>

            <div class="bg-white rounded-xl shadow-lg overflow-hidden mb-8">
                <div class="p-6 border-b border-gray-200 flex items-center justify-between">
                    <div class="flex items-center">
                        <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500"></div>
                    </div>
                    <span class="text-sm font-medium text-gray-500">更新示例</span>
                </div>
                <div class="p-6">
                    <h3 class="text-xl font-semibold mb-4">字段值更新</h3>
                    <div class="code-block p-4 rounded-lg mb-4 overflow-x-auto">
                        <pre class="text-sm text-gray-800">
POST /my_index/_update/1
{
  "script": {
    "source": "ctx._source.views += params.add_views",
    "params": {
      "add_views": 10
    }
  }
}</pre>
                    </div>
                    <div class="flex items-start mt-4">
                        <div class="bg-blue-100 p-2 rounded-full mr-3">
                            <i class="fas fa-info-circle text-blue-600"></i>
                        </div>
                        <p class="text-gray-600 flex-1">
                            在这个更新操作中，<code class="bg-gray-100 px-1 rounded">ctx._source.views += params.add_views</code> 将文档中的 <code class="bg-gray-100 px-1 rounded">views</code> 字段增加 10。<br>
                            <code class="bg-gray-100 px-1 rounded">params.add_views</code> 是传递给脚本的参数，用于动态设置增加的视图数量。
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Best Practices Section -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="bg-red-100 p-3 rounded-full mr-4">
                    <i class="fas fa-lightbulb text-red-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">自定义脚本的注意事项</h2>
            </div>

            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800">性能优化</h3>
                    <p class="text-gray-600 mb-4">使用脚本可能会影响性能，尤其是在大数据量和高并发的情况下。建议对脚本进行优化，避免复杂计算和不必要的操作。</p>
                    <ul class="space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>避免在脚本中执行重复计算</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>尽可能使用缓存参数</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>简化逻辑复杂度</span>
                        </li>
                    </ul>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800">安全实践</h3>
                    <p class="text-gray-600 mb-4">Painless 脚本在默认配置下禁用了不安全的操作，确保在写脚本时遵循安全最佳实践。</p>
                    <ul class="space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-shield-alt text-blue-500 mt-1 mr-2"></i>
                            <span>限制脚本访问的字段</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-shield-alt text-blue-500 mt-1 mr-2"></i>
                            <span>避免使用动态脚本</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-shield-alt text-blue-500 mt-1 mr-2"></i>
                            <span>严格控制脚本参数</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Complex Example Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8">复杂查询与自定义评分示例</h2>
            <div class="bg-white rounded-xl shadow-lg overflow-hidden">
                <div class="p-6 border-b border-gray-200 flex items-center justify-between">
                    <div class="flex items-center">
                        <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500"></div>
                    </div>
                    <span class="text-sm font-medium text-gray-500">复杂查询</span>
                </div>
                <div class="p-6">
                    <div class="code-block p-4 rounded-lg mb-4 overflow-x-auto">
                        <pre class="text-sm text-gray-800">
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "Elasticsearch"
          }
        }
      ],
      "filter": {
        "script_score": {
          "script": {
            "source": """
              double popularityScore = Math.log(2 + doc['popularity'].value);
              double ageScore = 1.0 / (1 + Math.pow(doc['age'].value, 2));
              return popularityScore * ageScore;
            """
          }
        }
      }
    }
  }
}</pre>
                    </div>
                    <div class="flex items-start mt-4">
                        <div class="bg-blue-100 p-2 rounded-full mr-3">
                            <i class="fas fa-info-circle text-blue-600"></i>
                        </div>
                        <p class="text-gray-600 flex-1">
                            在这个查询中，<code class="bg-gray-100 px-1 rounded">bool</code> 查询结合了 <code class="bg-gray-100 px-1 rounded">match</code> 和 <code class="bg-gray-100 px-1 rounded">script_score</code> 查询。<br>
                            <code class="bg-gray-100 px-1 rounded">script_score</code> 使用复杂的 Painless 脚本计算文档的自定义评分，结合了 <code class="bg-gray-100 px-1 rounded">popularity</code> 和 <code class="bg-gray-100 px-1 rounded">age</code> 字段。
                        </p>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-12">
        <div class="container mx-auto max-w-6xl px-4">
            <div class="flex flex-col items-center">
                <h3 class="text-xl font-bold mb-4">技术小馆</h3>
                <a href="http://www.yuque.com/jtostring" class="text-gray-400 hover:text-white transition duration-300">
                    <i class="fas fa-link mr-2"></i>http://www.yuque.com/jtostring
                </a>
                <div class="mt-6 flex space-x-4">
                    <a href="#" class="text-gray-400 hover:text-white transition duration-300">
                        <i class="fab fa-github text-xl"></i>
                    </a>
                    <a href="#" class="text-gray-400 hover:text-white transition duration-300">
                        <i class="fab fa-twitter text-xl"></i>
                    </a>
                    <a href="#" class="text-gray-400 hover:text-white transition duration-300">
                        <i class="fab fa-weixin text-xl"></i>
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            },
            themeVariables: {
                primaryColor: '#3a7bd5',
                primaryTextColor: '#fff',
                primaryBorderColor: '#3a7bd5',
                lineColor: '#3a7bd5',
                secondaryColor: '#006100',
                tertiaryColor: '#fff'
            }
        });

        // 微交互效果
        document.querySelectorAll('.feature-card').forEach(card => {
            card.addEventListener('mouseenter', function() {
                this.style.transition = 'transform 0.3s ease, box-shadow 0.3s ease';
            });
        });
    </script>
</body>
</html>
```